/*
 * 
 * 
 */
package com.pclong.net;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.*;
import java.net.*;

/**
 *@date 2011-11-7,22:49:47
 * @author long
 * @email 405409357@qq.com
 * @description ThreadServer类
 */
public class ThreadServer extends ServerSocket {

    private static final Logger logger = LogManager.getLogger(ThreadServer.class);
    private static final int SERVER_PORT = 10000;

    public ThreadServer() throws IOException {
        super(SERVER_PORT);

        try {
            while (true) {
                Socket socket = accept();
                new CreateServerThread(socket);
            }
        } catch (IOException e) {
        } finally {
            close();
        }
    }
//--- CreateServerThread 
    class CreateServerThread extends Thread {

        private Socket client;
        private BufferedReader in;
        private PrintWriter out;

        public CreateServerThread(Socket s) throws IOException {
            client = s;

            in = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));
            out = new PrintWriter(client.getOutputStream(), true);
            out.println("--- Welcome ---");
            start();
        }

        public void run() {
            try {
                String line = in.readLine();

                while (!line.equals("bye")) {
                    String msg = createMessage(line);
                    out.println(msg);
                    line = in.readLine();
                }
                out.println("--- See you, bye! ---");
                client.close();
            } catch (IOException e) {
            }
        }

        private String createMessage(String line) {
            logger.debug(line);
            return "abc";
        }
    }

    public static void main(String[] args) throws IOException {
//        new ThreadServer();
           while (true) {            
            System.out.println("2222");
        }
    }
}
